package com.hckjsec.law.controller;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.general.MapLongNameView;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.law.domain.*;
import com.hckjsec.law.domain.HomeView.*;
import com.hckjsec.law.domain.MonitorView.*;
import com.hckjsec.law.domain.VisitView.WechatLawView;
import com.hckjsec.law.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 公共法律服务咨询Controller
 *
 * @author cyt
 * @date 2021-11-08
 */
@RestController
@RequestMapping("/system/lawhome")
public class WebLawHomeController extends BaseController {
    public static Map<String,String> typeHt = new Hashtable<String,String>();
    static {
        if(typeHt.size()==0){
            typeHt.put("01","律师");
            typeHt.put("02","公证");
            typeHt.put("03","鉴定");
            typeHt.put("04","基层法律服务");
            typeHt.put("05","法援咨询");
        }
    }

    @Autowired
    private IWebLawConsullingService webLawConsullingService;

    @Autowired
    private ILawWechatUserService lawWechatUserService;

    @Autowired
    private IWebLawCompanyService webLawCompanyService;

    @Autowired
    private IWebLawReserveService webLawReserveService;

    @Autowired
    private IWebLawFlowService webLawFlowService;

    @Autowired
    private IWebLawEvaluateService webLawEvaluateService;

    @Autowired
    private IWebLawConsultationService webLawConsultationService;
    /**
     * 公法小程序端咨询列表-首页数据
     * @author cyt
     * @date 2021-06-23
     */
    @GetMapping("/getHomeNumber")
    public AjaxResult getHomeNumber() {
        LawHomePageView homePageView = new LawHomePageView();
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        //1.共解决问题
        WebLawConsulling webLawConsulling = new WebLawConsulling();
        webLawConsulling.getParams().put("number", "0");
        if (role < 8 ){
            webLawConsulling.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawConsulling> ConsullingList = webLawConsullingService.selectWebLawConsullingWechatList(webLawConsulling);
        homePageView.setTotal(ConsullingList.size());
        //2.本月新增问题
        WebLawConsulling webMonthLawConsulling = new WebLawConsulling();
        if (role < 8 ){
            webMonthLawConsulling.setDeptId(SecurityUtils.getDeptId());
        }
        String month = DateUtils.dateTimeNow(DateUtils.YYYY_MM)+"-01";
        webMonthLawConsulling.setBeginTime(month);
        List<WebLawConsulling> ConsullingMonthList = webLawConsullingService.selectWebLawConsullingWechatList(webMonthLawConsulling);
        homePageView.setMonthTotal(ConsullingMonthList.size());
        //3.本月解决问题
        webMonthLawConsulling.getParams().put("number", "0");
        List<WebLawConsulling> ConsullingMonthSolveList = webLawConsullingService.selectWebLawConsullingWechatList(webMonthLawConsulling);
        homePageView.setMonthSolveTotal(ConsullingMonthSolveList.size());
        //4.本月新增人员
        LawWechatUser lawWechatUser = new LawWechatUser();
        lawWechatUser.setBeginTime(month);
        List<LawWechatUser> lawWechatUserList = lawWechatUserService.selectLawWechatUserList(lawWechatUser);
        homePageView.setMonthUserTotal(lawWechatUserList.size());
        //5.类别展示
        WebLawCompany webLawCompany = new WebLawCompany();
        webLawCompany.setDeptId(SecurityUtils.getDeptId());
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        WebLawFlow webLawFlow = new  WebLawFlow();
        if (role < 8 && StringUtils.isNotEmpty(companyList)){
            webLawFlow.setCompanyType(companyList.get(0).getCompanyType());
        }
        List<WebLawFlow> flowList = webLawFlowService.selectWebLawFlowList(webLawFlow);
        List<MapLongNameView> list = new ArrayList<>();
        flowList.forEach(kv->{
            MapLongNameView mapLongView = new MapLongNameView();
            mapLongView.setName(typeHt.get(kv.getCompanyType()));
            mapLongView.setValue(kv.getQuantity());
            list.add(mapLongView);
        });
        homePageView.setList(list);
        return AjaxResult.success(homePageView);
    }


    /**
     * 公法小程序端咨询列表-首页监控
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/getHomeMonitor")
    public AjaxResult getHomeMonitor(@RequestBody WechatLawView wechatLawView) {
        LawPublicData lawPublicData = new LawPublicData("baoding","保定市","hbbd");
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawCompany webLawCompany = new WebLawCompany();
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<LawMonitorView> monitorViewList = new ArrayList<>();
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        companyList.forEach(kv->{
            LawMonitorView lawMonitorView = new LawMonitorView();
            lawMonitorView.setId(kv.getId());
            lawMonitorView.setType(kv.getCompanyType());
            lawMonitorView.setName(kv.getCompanyName());
            lawMonitorView.setValue(0);
            lawMonitorView.setValue1(0);
            lawMonitorView.setTotal(0);
            monitorViewList.add(lawMonitorView);
        });
        //1.咨询
        WebLawConsulling webLawConsulling = new WebLawConsulling();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawConsulling.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawConsulling.setEndTime(wechatLawView.getEndTime());
        }
        List<Map<String, Integer>> consullingListHt = webLawConsullingService.findCompanyIdByNumMap(webLawConsulling);
        Map<String,String> consullingMapHt = ComUtil.mapListByBigIntegerMap(consullingListHt,"company_id");
        monitorViewList.forEach(kv->{
            String idStr = String.valueOf(kv.getId());
            if (consullingMapHt.containsKey(idStr)){
                kv.setValue(Integer.valueOf(consullingMapHt.get(idStr)));
            }
            kv.plus();
        });
        //2.预约
        WebLawReserve webLawReserve = new WebLawReserve();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawReserve.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawReserve.setEndTime(wechatLawView.getEndTime());
        }
        List<Map<String, Integer>> reserveListHt = webLawReserveService.findCompanyIdByResMap(webLawReserve);
        Map<String,String> reserveMapHt = ComUtil.mapListByBigIntegerMap(reserveListHt,"company_id");
        monitorViewList.forEach(kv->{
            String idStr = String.valueOf(kv.getId());
            if (reserveMapHt.containsKey(idStr)){
                kv.setValue1(Integer.valueOf(reserveMapHt.get(idStr)));
            }
            kv.plus();
        });
        //3. 数据组装
        List<LawPublicOne> platData = new ArrayList<>();
        LawPublicOne  lawPublicOne = new LawPublicOne();
        lawPublicOne.setName("容城县");
        lawPublicOne.setValue(monitorViewList.stream().mapToInt(LawMonitorView::getValue).sum());
        lawPublicOne.setValue1(monitorViewList.stream().mapToInt(LawMonitorView::getValue1).sum());
        List<LawPublicNotaryData> notarydata = new ArrayList<>();
        List<MapLongNameView> abbdata = new ArrayList<>();
        monitorViewList.forEach(kv->{
            LawPublicNotaryData lawPublicNotaryData = new LawPublicNotaryData();
            lawPublicNotaryData.setName(kv.getName());
            lawPublicNotaryData.setValue(kv.getValue());
            lawPublicNotaryData.setValue1(kv.getValue1());
            notarydata.add(lawPublicNotaryData);
            MapLongNameView mapLongNameView = new MapLongNameView();
            mapLongNameView.setName(kv.getName());
            mapLongNameView.setValue(kv.getTotal().longValue());
            abbdata.add(mapLongNameView);
        });
        lawPublicOne.setNotarydata(notarydata);
        platData.add(lawPublicOne);
        lawPublicData.setPlatData(platData);
        lawPublicData.setAbbdata(abbdata);
        //分组求和
        Map<String,Integer> groupData = new HashMap<>();
        monitorViewList.forEach(kv->{
            int total = 0;
            String  groupName = kv.getType();
            if (groupData.containsKey(groupName)){
                total = groupData.get(groupName);
            }
            int findTotal = total + kv.getTotal();
            groupData.put(groupName,findTotal);
        });
        List<String> unitdataOne = new ArrayList<>();
        List<Integer> unitdataTwo = new ArrayList<>();
        AtomicInteger max = new AtomicInteger();
        groupData.forEach((k,v)->{
            unitdataOne.add(typeHt.get(k));
            if (v > max.get()){
                max.set(v);
            }
            unitdataTwo.add(v);
        });
        lawPublicData.setMax(max.get());
        LawPublicUnitData lawPublicUnitData = new LawPublicUnitData();
        lawPublicUnitData.setName(unitdataOne.toArray(new String[unitdataOne.size()]));
        lawPublicUnitData.setNumber(unitdataTwo.toArray(new Integer[unitdataTwo.size()]));
        lawPublicData.setUnitdata(lawPublicUnitData);
        return AjaxResult.success(lawPublicData);
    }

    /**
     * 公法小程序端咨询列表-首页数据
     * @author cyt
     * @date 2021-06-23
     */
    @GetMapping("/getHomeProblem")
    public AjaxResult getHomeProblem() {
        LawPublicProblemView problemView = new LawPublicProblemView();
        List<LawPublicProblemData> list = new ArrayList<>();
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawConsulling webMonthLawConsulling = new WebLawConsulling();
        if (role < 8 ){
            webMonthLawConsulling.setDeptId(SecurityUtils.getDeptId());
        }
        if (role < 7 ){
            webMonthLawConsulling.getParams().put("personal", SecurityUtils.getUserId());
        }
        webMonthLawConsulling.setState("0");
        List<WebLawConsulling> ConsullingList = webLawConsullingService.selectWebLawConsullingList(webMonthLawConsulling);
        ConsullingList.forEach(kv->{
            LawPublicProblemData lawPublicProblemData = new LawPublicProblemData();
            lawPublicProblemData.setId(kv.getId());
            lawPublicProblemData.setType("zx");
            lawPublicProblemData.setName(kv.getName());
            lawPublicProblemData.setTypeName(kv.getTypeName());
            lawPublicProblemData.setApplyTime(kv.getApplyTime());
            list.add(lawPublicProblemData);
        });
        WebLawReserve webLawReserve = new WebLawReserve();
        if (role < 8 ){
            webLawReserve.setDeptId(SecurityUtils.getDeptId());
        }
        if (role < 7 ){
            webLawReserve.getParams().put("personal", SecurityUtils.getUserId());
        }
        webLawReserve.setState("0");
        List<WebLawReserve> ReserveList = webLawReserveService.selectWebLawReserveList(webLawReserve);
        ReserveList.forEach(kv->{
            LawPublicProblemData lawPublicProblemData = new LawPublicProblemData();
            lawPublicProblemData.setId(kv.getId());
            lawPublicProblemData.setType("yy");
            lawPublicProblemData.setName(kv.getName());
            lawPublicProblemData.setTypeName(kv.getTypeName());
            lawPublicProblemData.setApplyTime(kv.getApplyTime());
            list.add(lawPublicProblemData);
        });
        list.sort(Comparator.comparing(LawPublicProblemData::getApplyTime).reversed());
        String operation = "false";
        if (role < 8 ){
            operation = "true";
        }
        problemView.setOperation(operation);
        problemView.setList(list);
        return AjaxResult.success(problemView);
    }


    /**
     * 公法小程序端-评价
     * @author cyt
     * @date 2021-06-23
     */
    @GetMapping("/getHomeEvaluate")
    public AjaxResult getHomeEvaluate() {
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawCompany webLawCompany = new WebLawCompany();
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        List<LawHomeEvaluate> lawHomeEvaluatelist = new ArrayList<LawHomeEvaluate>();
        companyList.forEach(kv->{
            BigDecimal totalAmount = new BigDecimal(0);
            LawHomeEvaluate lawHomeEvaluate = new LawHomeEvaluate();
            lawHomeEvaluate.setId(kv.getId());
            lawHomeEvaluate.setName(kv.getCompanyName());
            lawHomeEvaluate.setSatisfied(0L);
            lawHomeEvaluate.setVerySatisfied(0L);
            lawHomeEvaluate.setNoSatisfied(0L);
            lawHomeEvaluate.setTotal(0L);
            lawHomeEvaluatelist.add(lawHomeEvaluate);
        });
        List<WebLawEvaluate> webLawEvaluateList = webLawEvaluateService.selectWebLawEvaluateByConfigId(1L);
        webLawEvaluateList.forEach(kv->{
            lawHomeEvaluatelist.forEach(op->{
                 if (op.getId().equals(kv.getCompanyId())){
                     if (kv.getTotalEvaluate() == 0){
                         op.setSatisfied(kv.getConfigId());
                     }else if(kv.getTotalEvaluate() == 1){
                         op.setVerySatisfied(kv.getConfigId());
                     }else if(kv.getTotalEvaluate() == 2){
                         op.setNoSatisfied(kv.getConfigId());
                     }
                     op.plus();
                 }
            });
        });
        List<String> nameList = new ArrayList<>();
        List<Long> satisfiedList = new ArrayList<>();
        List<Long> verySatisfiedList  = new ArrayList<>();
        List<Long> noSatisfiedList = new ArrayList<>();
        lawHomeEvaluatelist.forEach(kv->{
            nameList.add(kv.getName()+"&总评价人数: "+kv.getTotal()+" 人");
            satisfiedList.add(kv.getSatisfied());
            verySatisfiedList.add(kv.getVerySatisfied());
            noSatisfiedList.add(kv.getNoSatisfied());
        });
        LawEvaluateOne  lawEvaluateOne = new LawEvaluateOne();
        lawEvaluateOne.setData(nameList.toArray(new String[nameList.size()]));
        List<LawEvaluateTwo> seriesList = new ArrayList<>();
        LawEvaluateTwo satisfied = new LawEvaluateTwo("满意","bar",0);
        satisfied.setData(satisfiedList.toArray(new Long[satisfiedList.size()]));
        seriesList.add(satisfied);
        LawEvaluateTwo verySatisfied = new LawEvaluateTwo("基本满意","bar",0);
        verySatisfied.setData(verySatisfiedList.toArray(new Long[verySatisfiedList.size()]));
        seriesList.add(verySatisfied);
        LawEvaluateTwo noSatisfied = new LawEvaluateTwo("不满意","bar",0);
        noSatisfied.setData(noSatisfiedList.toArray(new Long[noSatisfiedList.size()]));
        seriesList.add(noSatisfied);
        lawEvaluateOne.setSeries(seriesList);
        return AjaxResult.success(lawEvaluateOne);
    }

    /**
     * 公法小程序端-咨询列表
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/getConsullingReport")
    public AjaxResult getConsullingReport(@RequestBody WechatLawView wechatLawView) {
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawCompany webLawCompany = new WebLawCompany();
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        List<LawHomeConsulling> lawHomeConsullingList = new ArrayList<LawHomeConsulling>();
        companyList.forEach(kv->{
            LawHomeConsulling lawHomeConsulling = new LawHomeConsulling();
            lawHomeConsulling.setId(kv.getId());
            lawHomeConsulling.setName(kv.getCompanyName());
            lawHomeConsulling.setNoreply(0L);
            lawHomeConsulling.setReply(0L);
            lawHomeConsulling.setQuestioning(0L);
            lawHomeConsulling.setHasended(0L);
            lawHomeConsulling.setNoreplySubtotal(0L);
            lawHomeConsulling.setReplySubtotal(0L);
            lawHomeConsulling.setTotal(0L);
            lawHomeConsullingList.add(lawHomeConsulling);
        });
        WebLawConsulling webLawConsulling = new WebLawConsulling();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawConsulling.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawConsulling.setEndTime(wechatLawView.getEndTime());
        }
        List<WebLawConsulling> webLawConsullingList = webLawConsullingService.selectWebLawConsullingReportList(webLawConsulling);
        webLawConsullingList.forEach(kv->{
            lawHomeConsullingList.forEach(op->{
                if (op.getId().equals(kv.getCompanyId())){
                    if ("0".equals(kv.getState())){
                         op.setNoreply(kv.getConfigId());
                    }else if ("1".equals(kv.getState())){
                        op.setReply(kv.getConfigId());
                    }else if ("2".equals(kv.getState())){
                        op.setQuestioning(kv.getConfigId());
                    }else if ("3".equals(kv.getState())){
                        op.setHasended(kv.getConfigId());
                    }
                    op.plus();
                }
            });
        });
        List<String> nameList = new ArrayList<>();
        List<Long> noreplySubtotalList = new ArrayList<>();
        List<Long> replySubtotalList  = new ArrayList<>();
        lawHomeConsullingList.forEach(kv->{
            nameList.add(kv.getName()+"&总数: "+kv.getTotal()+" 条");
            noreplySubtotalList.add(kv.getNoreplySubtotal());
            replySubtotalList.add(kv.getReplySubtotal());
        });
        LawEvaluateThree lawEvaluateThree = new LawEvaluateThree();
        lawEvaluateThree.setData(nameList.toArray(new String[nameList.size()]));
        List<LawEvaluateTwo> seriesList = new ArrayList<>();
        LawEvaluateTwo noreplySubtotal = new LawEvaluateTwo("待回复","bar",0);
        noreplySubtotal.setData(noreplySubtotalList.toArray(new Long[noreplySubtotalList.size()]));
        seriesList.add(noreplySubtotal);
        LawEvaluateTwo replySubtotal = new LawEvaluateTwo("已回复","bar",0);
        replySubtotal.setData(replySubtotalList.toArray(new Long[replySubtotalList.size()]));
        seriesList.add(replySubtotal);
        lawEvaluateThree.setSeries(seriesList);
        lawEvaluateThree.setList(lawHomeConsullingList);
        return AjaxResult.success(lawEvaluateThree);
    }
    /**
     * 公法小程序端-咨询列表导出
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/exportConsullingReport")
    public AjaxResult exportConsullingReport(@RequestBody WechatLawView wechatLawView)
    {
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawCompany webLawCompany = new WebLawCompany();
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        List<LawHomeConsulling> lawHomeConsullingList = new ArrayList<LawHomeConsulling>();
        companyList.forEach(kv->{
            LawHomeConsulling lawHomeConsulling = new LawHomeConsulling();
            lawHomeConsulling.setId(kv.getId());
            lawHomeConsulling.setName(kv.getCompanyName());
            lawHomeConsulling.setNoreply(0L);
            lawHomeConsulling.setReply(0L);
            lawHomeConsulling.setQuestioning(0L);
            lawHomeConsulling.setHasended(0L);
            lawHomeConsulling.setNoreplySubtotal(0L);
            lawHomeConsulling.setReplySubtotal(0L);
            lawHomeConsulling.setTotal(0L);
            lawHomeConsullingList.add(lawHomeConsulling);
        });
        WebLawConsulling webLawConsulling = new WebLawConsulling();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawConsulling.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawConsulling.setEndTime(wechatLawView.getEndTime());
        }
        List<WebLawConsulling> webLawConsullingList = webLawConsullingService.selectWebLawConsullingReportList(webLawConsulling);
        webLawConsullingList.forEach(kv->{
            lawHomeConsullingList.forEach(op->{
                if (op.getId().equals(kv.getCompanyId())){
                    if ("0".equals(kv.getState())){
                        op.setNoreply(kv.getConfigId());
                    }else if ("1".equals(kv.getState())){
                        op.setReply(kv.getConfigId());
                    }else if ("2".equals(kv.getState())){
                        op.setQuestioning(kv.getConfigId());
                    }else if ("3".equals(kv.getState())){
                        op.setHasended(kv.getConfigId());
                    }
                    op.plus();
                }
            });
        });
        LawHomeConsulling lawHomeConsullingTotal = new LawHomeConsulling();
        lawHomeConsullingTotal.setName("  合    计  ");
        lawHomeConsullingTotal.setNoreply(lawHomeConsullingList.stream().mapToLong(LawHomeConsulling::getNoreply).sum());
        lawHomeConsullingTotal.setReply(lawHomeConsullingList.stream().mapToLong(LawHomeConsulling::getReply).sum());
        lawHomeConsullingTotal.setQuestioning(lawHomeConsullingList.stream().mapToLong(LawHomeConsulling::getQuestioning).sum());
        lawHomeConsullingTotal.setHasended(lawHomeConsullingList.stream().mapToLong(LawHomeConsulling::getHasended).sum());
        lawHomeConsullingTotal.setNoreplySubtotal(lawHomeConsullingList.stream().mapToLong(LawHomeConsulling::getNoreplySubtotal).sum());
        lawHomeConsullingTotal.setReplySubtotal(lawHomeConsullingList.stream().mapToLong(LawHomeConsulling::getReplySubtotal).sum());
        lawHomeConsullingTotal.setTotal(lawHomeConsullingList.stream().mapToLong(LawHomeConsulling::getTotal).sum());
        lawHomeConsullingList.add(lawHomeConsullingTotal);
        ExcelUtil<LawHomeConsulling> util = new ExcelUtil<LawHomeConsulling>(LawHomeConsulling.class);
        return util.exportExcel(lawHomeConsullingList, "咨询");
    }


    /**
     * 公法小程序端-预约列表
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/getReserveReport")
    public AjaxResult getReserveReport(@RequestBody WechatLawView wechatLawView) {
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawCompany webLawCompany = new WebLawCompany();
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        List<LawHomeReserve> lawHomeReserveList = new ArrayList<LawHomeReserve>();
        companyList.forEach(kv->{
            LawHomeReserve lawHomeReserve = new LawHomeReserve();
            lawHomeReserve.setId(kv.getId());
            lawHomeReserve.setName(kv.getCompanyName());
            lawHomeReserve.setToBeReviewed(0L);
            lawHomeReserve.setReviewed(0L);
            lawHomeReserve.setRefuse(0L);
            lawHomeReserve.setTotal(0L);
            lawHomeReserveList.add(lawHomeReserve);
        });
        WebLawReserve webLawReserve = new WebLawReserve();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawReserve.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawReserve.setEndTime(wechatLawView.getEndTime());
        }
        List<WebLawReserve> webLawReserveList = webLawReserveService.selectWebLawReserveReportList(webLawReserve);
        webLawReserveList.forEach(kv->{
            lawHomeReserveList.forEach(op->{
                if (op.getId().equals(kv.getCompanyId())){
                    if ("0".equals(kv.getState())){
                        op.setToBeReviewed(kv.getConfigId());
                    }else if ("1".equals(kv.getState())){
                        op.setReviewed(kv.getConfigId());
                    }else if ("2".equals(kv.getState())){
                        op.setRefuse(kv.getConfigId());
                    }
                    op.plus();
                }
            });
        });
        List<String> nameList = new ArrayList<>();
        List<Long> toBeReviewedList = new ArrayList<>();
        List<Long> reviewedList  = new ArrayList<>();
        List<Long> refuseList  = new ArrayList<>();
        lawHomeReserveList.forEach(kv->{
            nameList.add(kv.getName()+"&总数: "+kv.getTotal()+" 条");
            toBeReviewedList.add(kv.getToBeReviewed());
            reviewedList.add(kv.getReviewed());
            refuseList.add(kv.getRefuse());
        });
        LawEvaluateFour lawEvaluateFour = new LawEvaluateFour();
        lawEvaluateFour.setData(nameList.toArray(new String[nameList.size()]));
        List<LawEvaluateTwo> seriesList = new ArrayList<>();
        LawEvaluateTwo toBeReviewed = new LawEvaluateTwo("待审核","bar",0);
        toBeReviewed.setData(toBeReviewedList.toArray(new Long[toBeReviewedList.size()]));
        seriesList.add(toBeReviewed);
        LawEvaluateTwo reviewed = new LawEvaluateTwo("已同意","bar",0);
        reviewed.setData(reviewedList.toArray(new Long[reviewedList.size()]));
        seriesList.add(reviewed);
        LawEvaluateTwo refuse = new LawEvaluateTwo("已拒绝","bar",0);
        refuse.setData(refuseList.toArray(new Long[refuseList.size()]));
        seriesList.add(refuse);
        lawEvaluateFour.setSeries(seriesList);
        lawEvaluateFour.setList(lawHomeReserveList);
        return AjaxResult.success(lawEvaluateFour);
    }


    /**
     * 公法小程序端-预约列表导出
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/exportReserveReport")
    public AjaxResult exportReserveReport(@RequestBody WechatLawView wechatLawView) {
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawCompany webLawCompany = new WebLawCompany();
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        List<LawHomeReserve> lawHomeReserveList = new ArrayList<LawHomeReserve>();
        companyList.forEach(kv->{
            LawHomeReserve lawHomeReserve = new LawHomeReserve();
            lawHomeReserve.setId(kv.getId());
            lawHomeReserve.setName(kv.getCompanyName());
            lawHomeReserve.setToBeReviewed(0L);
            lawHomeReserve.setReviewed(0L);
            lawHomeReserve.setRefuse(0L);
            lawHomeReserve.setTotal(0L);
            lawHomeReserveList.add(lawHomeReserve);
        });
        WebLawReserve webLawReserve = new WebLawReserve();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawReserve.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawReserve.setEndTime(wechatLawView.getEndTime());
        }
        List<WebLawReserve> webLawReserveList = webLawReserveService.selectWebLawReserveReportList(webLawReserve);
        webLawReserveList.forEach(kv->{
            lawHomeReserveList.forEach(op->{
                if (op.getId().equals(kv.getCompanyId())){
                    if ("0".equals(kv.getState())){
                        op.setToBeReviewed(kv.getConfigId());
                    }else if ("1".equals(kv.getState())){
                        op.setReviewed(kv.getConfigId());
                    }else if ("2".equals(kv.getState())){
                        op.setRefuse(kv.getConfigId());
                    }
                    op.plus();
                }
            });
        });
        LawHomeReserve lawHomeReserveTotal = new LawHomeReserve();
        lawHomeReserveTotal.setName("  合    计  ");
        lawHomeReserveTotal.setToBeReviewed(lawHomeReserveList.stream().mapToLong(LawHomeReserve::getToBeReviewed).sum());
        lawHomeReserveTotal.setReviewed(lawHomeReserveList.stream().mapToLong(LawHomeReserve::getReviewed).sum());
        lawHomeReserveTotal.setRefuse(lawHomeReserveList.stream().mapToLong(LawHomeReserve::getRefuse).sum());
        lawHomeReserveList.add(lawHomeReserveTotal);
        ExcelUtil<LawHomeReserve> util = new ExcelUtil<LawHomeReserve>(LawHomeReserve.class);
        return util.exportExcel(lawHomeReserveList, "预约");
    }


    /**
     * 公法小程序端-评价报表
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/getEvaluateReport")
    public AjaxResult getEvaluateReport(@RequestBody WechatLawView wechatLawView) {
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawCompany webLawCompany = new WebLawCompany();
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        List<LawHomeEvaluate> lawHomeEvaluatelist = new ArrayList<LawHomeEvaluate>();
        companyList.forEach(kv->{
            BigDecimal totalAmount = new BigDecimal(0);
            LawHomeEvaluate lawHomeEvaluate = new LawHomeEvaluate();
            lawHomeEvaluate.setId(kv.getId());
            lawHomeEvaluate.setName(kv.getCompanyName());
            lawHomeEvaluate.setSatisfied(0L);
            lawHomeEvaluate.setVerySatisfied(0L);
            lawHomeEvaluate.setNoSatisfied(0L);
            lawHomeEvaluate.setTotal(0L);
            lawHomeEvaluatelist.add(lawHomeEvaluate);
        });
        WebLawEvaluate webLawEvaluate = new WebLawEvaluate();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawEvaluate.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawEvaluate.setEndTime(wechatLawView.getEndTime());
        }
        List<WebLawEvaluate> webLawEvaluateList = webLawEvaluateService.selectWebLawEvaluateReportList(webLawEvaluate);
        webLawEvaluateList.forEach(kv->{
            lawHomeEvaluatelist.forEach(op->{
                if (op.getId().equals(kv.getCompanyId())){
                    if (kv.getTotalEvaluate() == 0){
                        op.setSatisfied(kv.getConfigId());
                    }else if(kv.getTotalEvaluate() == 1){
                        op.setVerySatisfied(kv.getConfigId());
                    }else if(kv.getTotalEvaluate() == 2){
                        op.setNoSatisfied(kv.getConfigId());
                    }
                    op.plus();
                }
            });
        });
        List<String> nameList = new ArrayList<>();
        List<Long> satisfiedList = new ArrayList<>();
        List<Long> verySatisfiedList  = new ArrayList<>();
        List<Long> noSatisfiedList = new ArrayList<>();
        lawHomeEvaluatelist.forEach(kv->{
            nameList.add(kv.getName()+"&总评价人数: "+kv.getTotal()+" 人");
            satisfiedList.add(kv.getSatisfied());
            verySatisfiedList.add(kv.getVerySatisfied());
            noSatisfiedList.add(kv.getNoSatisfied());
        });
        LawEvaluateOne  lawEvaluateOne = new LawEvaluateOne();
        lawEvaluateOne.setData(nameList.toArray(new String[nameList.size()]));
        List<LawEvaluateTwo> seriesList = new ArrayList<>();
        LawEvaluateTwo satisfied = new LawEvaluateTwo("满意","bar",0);
        satisfied.setData(satisfiedList.toArray(new Long[satisfiedList.size()]));
        seriesList.add(satisfied);
        LawEvaluateTwo verySatisfied = new LawEvaluateTwo("基本满意","bar",0);
        verySatisfied.setData(verySatisfiedList.toArray(new Long[verySatisfiedList.size()]));
        seriesList.add(verySatisfied);
        LawEvaluateTwo noSatisfied = new LawEvaluateTwo("不满意","bar",0);
        noSatisfied.setData(noSatisfiedList.toArray(new Long[noSatisfiedList.size()]));
        seriesList.add(noSatisfied);
        lawEvaluateOne.setSeries(seriesList);
        lawEvaluateOne.setList(lawHomeEvaluatelist);
        return AjaxResult.success(lawEvaluateOne);
    }


    /**
     * 公法小程序端-评价报表导出
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/exportEvaluateReport")
    public AjaxResult exportEvaluateReport(@RequestBody WechatLawView wechatLawView) {
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        WebLawCompany webLawCompany = new WebLawCompany();
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> companyList = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        List<LawHomeEvaluate> lawHomeEvaluatelist = new ArrayList<LawHomeEvaluate>();
        companyList.forEach(kv->{
            BigDecimal totalAmount = new BigDecimal(0);
            LawHomeEvaluate lawHomeEvaluate = new LawHomeEvaluate();
            lawHomeEvaluate.setId(kv.getId());
            lawHomeEvaluate.setName(kv.getCompanyName());
            lawHomeEvaluate.setSatisfied(0L);
            lawHomeEvaluate.setVerySatisfied(0L);
            lawHomeEvaluate.setNoSatisfied(0L);
            lawHomeEvaluate.setTotal(0L);
            lawHomeEvaluatelist.add(lawHomeEvaluate);
        });
        WebLawEvaluate webLawEvaluate = new WebLawEvaluate();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawEvaluate.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawEvaluate.setEndTime(wechatLawView.getEndTime());
        }
        List<WebLawEvaluate> webLawEvaluateList = webLawEvaluateService.selectWebLawEvaluateReportList(webLawEvaluate);
        webLawEvaluateList.forEach(kv->{
            lawHomeEvaluatelist.forEach(op->{
                if (op.getId().equals(kv.getCompanyId())){
                    if (kv.getTotalEvaluate() == 0){
                        op.setSatisfied(kv.getConfigId());
                    }else if(kv.getTotalEvaluate() == 1){
                        op.setVerySatisfied(kv.getConfigId());
                    }else if(kv.getTotalEvaluate() == 2){
                        op.setNoSatisfied(kv.getConfigId());
                    }
                    op.plus();
                }
            });
        });
        LawHomeEvaluate lawHomeEvaluateTotal = new LawHomeEvaluate();
        lawHomeEvaluateTotal.setName("  合    计  ");
        lawHomeEvaluateTotal.setSatisfied(lawHomeEvaluatelist.stream().mapToLong(LawHomeEvaluate::getSatisfied).sum());
        lawHomeEvaluateTotal.setVerySatisfied(lawHomeEvaluatelist.stream().mapToLong(LawHomeEvaluate::getVerySatisfied).sum());
        lawHomeEvaluateTotal.setNoSatisfied(lawHomeEvaluatelist.stream().mapToLong(LawHomeEvaluate::getNoSatisfied).sum());
        lawHomeEvaluateTotal.setTotal(lawHomeEvaluatelist.stream().mapToLong(LawHomeEvaluate::getTotal).sum());
        lawHomeEvaluatelist.add(lawHomeEvaluateTotal);
        ExcelUtil<LawHomeEvaluate> util = new ExcelUtil<LawHomeEvaluate>(LawHomeEvaluate.class);
        return util.exportExcel(lawHomeEvaluatelist, "满意度评价");
    }



    /**
     * 公法小程序端-政务服务列表
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/getConsultationReport")
    public AjaxResult getConsultationReport(@RequestBody WechatLawView wechatLawView) {
        List<LawHomeConsultation> lawHomeConsultationList = new ArrayList<>();
        lawHomeConsultationList.add(new LawHomeConsultation("0","咨询",0L,0L,0L));
        lawHomeConsultationList.add(new LawHomeConsultation("1","投诉",0L,0L,0L));
        WebLawConsultation webLawConsultation = new WebLawConsultation();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawConsultation.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawConsultation.setEndTime(wechatLawView.getEndTime());
        }
        List<WebLawConsultation>  webLawConsultationList = webLawConsultationService.selectWebLawReportList(webLawConsultation);
        webLawConsultationList.forEach(kv->{
            lawHomeConsultationList.forEach(op->{
                if (op.getType().equals(kv.getType())){
                   if ("0".equals(kv.getState())){
                      op.setPending(kv.getId());
                   }else if ("1".equals(kv.getState())){
                      op.setProcessed(kv.getId());
                   }
                   op.plus();
                }
            });
        });
        List<String> nameList = new ArrayList<>();
        List<Long> pendingList = new ArrayList<>();
        List<Long> processedList  = new ArrayList<>();
        lawHomeConsultationList.forEach(kv->{
            nameList.add(kv.getName()+"&总数: "+kv.getTotal()+" 条");
            pendingList.add(kv.getPending());
            processedList.add(kv.getProcessed());
        });
        LawEvaluateSix lawEvaluateSix = new LawEvaluateSix();
        lawEvaluateSix.setData(nameList.toArray(new String[nameList.size()]));
        List<LawEvaluateTwo> seriesList = new ArrayList<>();
        LawEvaluateTwo pending = new LawEvaluateTwo("待处理","bar",0);
        pending.setData(pendingList.toArray(new Long[pendingList.size()]));
        seriesList.add(pending);
        LawEvaluateTwo processed = new LawEvaluateTwo("已处理","bar",0);
        processed.setData(processedList.toArray(new Long[processedList.size()]));
        seriesList.add(processed);
        lawEvaluateSix.setSeries(seriesList);
        lawEvaluateSix.setList(lawHomeConsultationList);
        return AjaxResult.success(lawEvaluateSix);
    }


    /**
     * 公法小程序端-政务服务列表导出
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping(value = "/exportConsultationReport")
    public AjaxResult exportConsultationReport(@RequestBody WechatLawView wechatLawView) {
        List<LawHomeConsultation> lawHomeConsultationList = new ArrayList<>();
        lawHomeConsultationList.add(new LawHomeConsultation("0","咨询",0L,0L,0L));
        lawHomeConsultationList.add(new LawHomeConsultation("1","投诉",0L,0L,0L));
        WebLawConsultation webLawConsultation = new WebLawConsultation();
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getBeginTime(),""))){
            webLawConsultation.setBeginTime(wechatLawView.getBeginTime());
        }
        if(!"".equals(StringUtils.isHcNull(wechatLawView.getEndTime(),""))){
            webLawConsultation.setEndTime(wechatLawView.getEndTime());
        }
        List<WebLawConsultation>  webLawConsultationList = webLawConsultationService.selectWebLawReportList(webLawConsultation);
        webLawConsultationList.forEach(kv->{
            lawHomeConsultationList.forEach(op->{
                if (op.getType().equals(kv.getType())){
                    if ("0".equals(kv.getState())){
                        op.setPending(kv.getId());
                    }else if ("1".equals(kv.getState())){
                        op.setProcessed(kv.getId());
                    }
                    op.plus();
                }
            });
        });

        LawHomeConsultation lawHomeEvaluateTotal = new LawHomeConsultation();
        lawHomeEvaluateTotal.setName("  合    计  ");
        lawHomeEvaluateTotal.setPending(lawHomeConsultationList.stream().mapToLong(LawHomeConsultation::getPending).sum());
        lawHomeEvaluateTotal.setProcessed(lawHomeConsultationList.stream().mapToLong(LawHomeConsultation::getProcessed).sum());
        lawHomeEvaluateTotal.setTotal(lawHomeConsultationList.stream().mapToLong(LawHomeConsultation::getTotal).sum());
        lawHomeConsultationList.add(lawHomeEvaluateTotal);
        ExcelUtil<LawHomeConsultation> util = new ExcelUtil<LawHomeConsultation>(LawHomeConsultation.class);
        return util.exportExcel(lawHomeConsultationList, "政务服务咨询");
    }
}
